package com.lc.demo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lc.demo.bean.*;
import com.lc.demo.result.ResponseMessage;
import com.lc.demo.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName AdminController
 * @Deacription TODO
 * @Author jinshuhan
 * @Version 1.0
 **/
@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private ScoreService scoreService;

    @Autowired
    private SubjectService subjectService;

    @PostMapping(value = "/login")
    public ResponseMessage login(@RequestBody Admin admin) {
        Admin adm = adminService.login(admin.getUsername(), admin.getPassword());
        if (adm != null) {
            return ResponseMessage.successWithMessage(adm.getUsername());
        } else {
            return ResponseMessage.errorWithMessage("用户名或密码错误");
        }
    }


    @PostMapping(value = "/getScoreList")
    public ResponseMessage findScoreList(@RequestBody Score score) {

        int pageNum = 1;
        int pageSize = 10;
        if (score.getPageNum() != null) {
            pageNum = score.getPageNum();
        }
        if (score.getPageSize() != null) {
            pageSize = score.getPageSize();
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Score> scoreList = scoreService.findScoreListByUsername(score, score.getUsername());
        PageInfo<Score> page = new PageInfo<>(scoreList);
        return ResponseMessage.successWithData(page);
    }


    @PostMapping(value = "/adminReview")
    public ResponseMessage review(@RequestBody Score score) {
        //审核通过 2  审核不通过状态1
        scoreService.adminReview(score);
        return ResponseMessage.success();
    }

    @Autowired
    ClassGradleService classGradleService;


   /* //添加课程
    @PostMapping(value = "/addSubject")
    public ResponseMessage addSubject(@RequestBody Subject subject, String classgradeids, String username) {
        subjectService.addSubjectByUsername(subject, classgradeids, username);
        return ResponseMessage.success();
    }*/

    //添加课程
    @PostMapping(value = "/addSubject")
    public ResponseMessage addSubject(@RequestBody Subject subject) {
        subjectService.addSubjectByUsername(subject);
        return ResponseMessage.success();
    }

    /*//修改课程
    @PostMapping(value = "/updateSubject")
    public ResponseMessage updateSubject(@RequestBody Subject subject, String classgradeids, String username) {
        subjectService.updateSubject(subject, classgradeids, username);
        return ResponseMessage.success();
    }*/

    //修改课程
    @PostMapping(value = "/updateSubject")
    public ResponseMessage updateSubject(@RequestBody Subject subject) {
        subjectService.updateSubject(subject);
        return ResponseMessage.success();
    }

    //查询课程列表
    @PostMapping(value = "/subjectList")
    public ResponseMessage subjectList(@RequestBody Subject subject) {

        int pageNum = 1;
        int pageSize = 10;
        if (subject.getPageNum() != null) {
            pageNum = subject.getPageNum();
        }
        if (subject.getPageSize() != null) {
            pageSize = subject.getPageSize();
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Subject> studentList = subjectService.getSubjectList(subject.getUsername());
        PageInfo<Subject> page = new PageInfo<>(studentList);
        return ResponseMessage.successWithData(page);
    }


    //删除课程
    @PostMapping(value = "/delSubject")
    public ResponseMessage delSubject(@RequestBody Subject subject) {
        subjectService.delSubject(subject);
        return ResponseMessage.success();
    }
}
